using System.Web.Mvc;
using good1.ApplicationServices.Interface;
using good1.Core;
using good1.Core.Enums;
using good1.Web.Controllers.Attributes;
using good1.Web.Controllers.Infrastructure;
using SharpArch.Core.PersistenceSupport;
using System.Collections.Generic;
using SharpArch.Web.NHibernate;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace good1.Web.Controllers
{
    [HandleError]
    [AuthorizeAttributeCustom(RoleRequired = GoodRoles.Nimda)]
    public class TagsController : BaseController
    {
        public TagsController(IRepository<Tag> tagRepository, ISecurityContextService securityContext) : base(securityContext)
        {
            Check.Require(tagRepository != null, "tagRepository may not be null");

            this.tagRepository = tagRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<Tag> tags = tagRepository.GetAll();
            return View(tags);
        }

        [Transaction]
        public ActionResult Show(int id) {
            Tag tag = tagRepository.Get(id);
            return View(tag);
        }

        public ActionResult Create() {
            return View();
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Tag tag) {
            if (tag.IsValid()) {
                tagRepository.SaveOrUpdate(tag);

                TempData["message"] = "The tag was successfully created.";
                return RedirectToAction("Index");
            }

            MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState,
                tag.ValidationResults());
            return View();
        }

        [Transaction]
        public ActionResult Edit(int id) {
            Tag tag = tagRepository.Get(id);
            return View(tag);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, [ModelBinder(typeof(DefaultModelBinder))] Tag tag) {
            Tag tagToUpdate = tagRepository.Get(id);
            TransferFormValuesTo(tagToUpdate, tag);

            if (tagToUpdate.IsValid()) {
                TempData["message"] = "The tag was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                tagRepository.DbContext.RollbackTransaction();
                MvcValidationAdapter.TransferValidationMessagesTo(ViewData.ModelState, 
                    tagToUpdate.ValidationResults());
                return View(tagToUpdate);
            }
        }

        private void TransferFormValuesTo(Tag tagToUpdate, Tag tagFromForm) {
			tagToUpdate.Name = tagFromForm.Name;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The tag was successfully deleted.";
            Tag tagToDelete = tagRepository.Get(id);

            if (tagToDelete != null) {
                tagRepository.Delete(tagToDelete);

                try {
                    tagRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the tag from being deleted. " +
						"Another item likely depends on this tag.";
                    tagRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The tag could not be found for deletion. It may already have been deleted.";
            }

            TempData["Message"] = resultMessage;
            return RedirectToAction("Index");
        }

        private readonly IRepository<Tag> tagRepository;
    }
}
